<?php
class Todo extends Controller {
	var $base;
	var $css;
	
	function Todo()
	{
		parent::Controller();	
		$this->base = $this->config->item('base_url');
		$this->css = $this->config->item('css');
		$this->load->model('Todo_model');
		$this->load->helper('url');
		$this->load->library('session');
	}
	
	function index()
	{
		$data['css'] = $this->css;
		$data['base'] = $this->base;
//		$this->load->database();
		$data['flash_message'] = $this->session->flashdata('message');	
		$data['todo_list'] = $this->Todo_model->getNextTodo();
        $data['completed_list'] = $this->Todo_model->getCompletedTodo();
        $this->load->view('index', $data);		
	}
	
	function add()
	{
		$this->load->library('form_validation');
		//validation rule
		$this->form_validation->set_rules('title','Title','required');
		$this->form_validation->set_rules('description','Description','max_length[25]');
		
		if($this->form_validation->run())
		{
			$todo = array(
						'name'=> $this->input->post('title'),
						'description'=> $this->input->post('description')
						);
			$this->Todo_model->add($todo);
			$this->session->set_flashdata('message', 'Done. You have added new task');
			redirect('/');
		}
		else
		{
			$data['css'] = $this->css;
			$data['base'] = $this->base;
			$this->load->view('add',$data);
		}
	}
	
	function delete($id)
	{
		$data=$this->Todo_model->get($id);
		
		if ($this->Todo_model->delete($id)) {
            $this->session->set_flashdata('message', "Done. You have deleted $data->name.");                        
        } else {
            $this->session->set_flashdata('message', "No data found. You deleted wrong to do list."); 
        }
        redirect('');
 
    }
 
    function complete($id)
    {
        $data = $this->Todo_model->get($id);
 
        if ($this->Todo_model->setComplete($id)) {
            $this->session->set_flashdata('message', "You have set $data->name as complete.");                        
        } else {
            $this->session->set_flashdata('message', "No data found. You access wrong to do list.");  
        }
        redirect('');
    }
}
?>